package com.rc.saas.tenant.controller.fault;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.base.TenantBaseController;
import com.rc.saas.tenant.base.WebPageInfo;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.model.device.RcLabel;
import com.rc.saas.tenant.model.fault.RcElecPileFault;
import com.rc.saas.tenant.model.tenant.TenantCompanyInfo;
import com.rc.saas.tenant.model.tenant.TenantRole;
import com.rc.saas.tenant.model.tenant.TenantUser;
import com.rc.saas.tenant.search.elec.ElecLabelSearch;
import com.rc.saas.tenant.search.fault.RcElecPileFaultSearch;
import com.rc.saas.tenant.search.tenant.TenantRoleSearch;
import com.rc.saas.tenant.service.elec.ElecLabelService;
import com.rc.saas.tenant.service.fault.RcElecPileFaultService;
import com.rc.saas.tenant.service.tenant.TenantCompanyInfoService;
import com.rc.saas.tenant.shiro.ShiroUtils;
import com.rc.saas.tenant.util.ExportUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;


/**
 * zhoushiyu
 * 站标签
 */
@Controller
@RequestMapping("pileFault")
public class RcElecPileFaultController extends TenantBaseController {
    private static Logger logger = LogManager.getLogger(RcElecPileFaultController.class);

    @Autowired
    private RcElecPileFaultService rcElecPileFaultService;


    /**
     * 跳转站标签页面
     *
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String pileFaultPage(Model model) {
        return "/templates/fault/pileFault";
    }

    /**
     * 返回桩群列表数据
     *
     * @return
     */
    @RequestMapping(value = "/listPileFault", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listPileFault(RcElecPileFaultSearch rcElecPileFaultSearch) {
        PageInfo<RcElecPileFault> pageInfo = rcElecPileFaultService.listPageByCondition(rcElecPileFaultSearch);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }

    /**
     * 导出故障列表
     */
    @ResponseBody
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public TResponse export(RcElecPileFaultSearch rcElecPileFaultSearch, HttpServletResponse response) {
        try {
            response.setHeader("Set-Cookie", "fileDownload=true; path=/");
            ExportUtils.setDefaultPaging(rcElecPileFaultSearch);
            PageInfo<RcElecPileFault> pageInfo = rcElecPileFaultService.listPageByCondition(rcElecPileFaultSearch);
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("故障列表", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), RcElecPileFault.class).sheet("故障列表").doWrite(pageInfo.getList());
            return null;
        } catch (Exception e) {
            logger.error("导出故障列表失败", e.getMessage());
            return TResponse.FAIL(e.getMessage());
        }
    }

    /**
     * 跳转增加桩群
     *
     * @return
     */
    @RequestMapping(value = "/addPileFault", method = RequestMethod.GET)
    public String addPileFault(Model model) {
        return "/templates/fault/addPileFault";
    }

    /**
     * 保存编辑桩群
     *
     * @return
     */
    @RequestMapping(value = "/saveOrUpdatePileFault", method = RequestMethod.POST)
    @ResponseBody
    public TResponse saveOrUpdatePileFault(RcElecPileFault rcElecPileFault){
        TenantUser user = getSessionTenantUser();
        try {
            RcElecPileFault info = rcElecPileFaultService.selectByPrimaryKey(rcElecPileFault.getPileFaultId());
            if (info!=null){
                info.setPileFaultLevel(rcElecPileFault.getPileFaultLevel());
                info.setPileFaultDetail(rcElecPileFault.getPileFaultDetail());
                info.setPileFaultType(rcElecPileFault.getPileFaultType());
                info.setLastUpdatedTime(new Date());
                info.setLastUpdatedBy(user.getLoginName());
                rcElecPileFaultService.updateByPrimaryKey(info);
            }else{
                rcElecPileFault.setStatus(1);
                rcElecPileFault.setCreatedTime(new Date());
                rcElecPileFault.setCreatedBy(user.getLoginName());
                rcElecPileFaultService.insert(rcElecPileFault);
            }
            return TResponse.SUCCESS();
        }catch (Exception e){
            logger.error("保存故障失败", e);
            return TResponse.FAIL("保存故障失败");
        }
    }

    /**
     * 跳转编辑桩群
     *
     * @return
     */
    @RequestMapping(value = "/editPileFault/{pileFaultId}", method = RequestMethod.GET)
    public String editPileFault(@PathVariable("pileFaultId") Long pileFaultId, Model model) {
        RcElecPileFault info = rcElecPileFaultService.selectByPrimaryKey(pileFaultId);
        model.addAttribute("pileFault", info);
        return "/templates/fault/editPileFault";
    }

    /**
     * 保存编辑桩群电价
     *
     * @return
     */
    @RequestMapping(value = "/deletePileFault", method = RequestMethod.POST)
    @ResponseBody
    public TResponse deletePileFault(Long pileFaultId) {
        RcElecPileFault info = rcElecPileFaultService.selectByPrimaryKey(pileFaultId);
        if (info==null){
            return TResponse.FAIL("请求错误");
        }
        rcElecPileFaultService.deleteByPrimaryKey(pileFaultId);
        return TResponse.SUCCESS();
    }

}
